﻿package cn.valorin.command.sub;

import java.io.File;
import java.util.List;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cn.valorin.Main;
import cn.valorin.command.SubCommand;
import cn.valorin.configuration.languagefile.LanguageFileLoader;

import static cn.valorin.configuration.languagefile.MessageSender.*;
import static cn.valorin.configuration.Configuration.*;

public class Changelang extends SubCommand{

	public Changelang() {
		super("changelang","cl");
	}

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) 
	{
	    Player p = null;
	    if (sender instanceof Player)
	    { p = (Player)sender; }
	    if (args.length == 1)  {
	        LanguageFileLoader lfl = Main.getInstance().getLanguageFileLoader();
	        List<File> flist = lfl.getLanguagesList();
	        if (flist.size() == 0) {
		        sm("&c服务器不存在任何语言文件，如您有特殊需求请联系腐竹",p);
	        } else { 
	    	    sm("&6语言文件如下",p);
		        for (File f : flist) {
		            sender.sendMessage("§e"+f.getName().replace(".txt", "")+"§7("+
		                     gm("简体中文",f,0)+"§7)");
		        }
		        sm("&6共计 &f&l{amount} &6个",p,"amount",new String[]{""+flist.size()});
	        }
	        return true;
	    }
	    if (args.length == 2) {
		    if (p == null) {
                sm("&c这条指令只能由服务器内的玩家执行！后台无法使用！",p);
                return true;
            }
		    LanguageFileLoader lfl = Main.getInstance().getLanguageFileLoader();
		    List<File> flist = lfl.getLanguagesList();
		    if (flist.size() != 0) {
		        for (File f : flist) {
	                if (args[1].equalsIgnoreCase(f.getName().replace(".txt", ""))) {
	    	            data.set("PlayerData."+p.getName()+".Language", args[1]);
	    	            saveData();
	    	            sm("&a语言环境切换已完成",p);
	    	            return true;
	                }
		        }
		        sm("&c语言文件&e{name}&c不存在",p,"name",new String[]{""+args[1]});
		    } else {
		        sm("&c语言文件&e{name}&c不存在",p,"name",new String[]{""+args[1]});
		    }
	  }
	  return true;
	}

}
